﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Collections;

namespace System.Web.Utils {

    public abstract class Controller {

        #region Fileds
        public IDictionary Data = new Hashtable();
        private static ITemplateEngine fileTemplateEngine = null;
        private static ITemplateEngine memoryTemplateEngine = null;
        #endregion

        public abstract void Index();

        public Controller() {}

        public void GoToUrl(string UrlPath) {
            HttpContext.Current.Response.Redirect(UrlPath);
        }

        #region Render Methods

        private string RenderFileProcess(IDictionary HasTableData, string TemplateName){
            if(fileTemplateEngine == null) fileTemplateEngine = TemplateEngineFactory.CreateTemplateFileEngine(string.Format(@"{0}\views", HttpContext.Current.Request.PhysicalApplicationPath), true);
            IDictionary temData = HasTableData != null ? HasTableData : Data;
            string strReturn = fileTemplateEngine.Process(temData, TemplateName);
            return strReturn;
        }

        public void RenderFile(string TemplateName) {
            HttpContext.Current.Response.Write(RenderFileProcess(null, TemplateName));
        }

        public void RenderFile(IDictionary HasTableData, string TemplateName) {
            HttpContext.Current.Response.Write(RenderFileProcess(HasTableData, TemplateName));
        }

        private string RenderStringProcess(IDictionary HasTableData, string TemplateName) {
            if(memoryTemplateEngine == null) memoryTemplateEngine = TemplateEngineFactory.CreateTemplateMemoryEngine(true);
            
            IDictionary temData = HasTableData != null ? HasTableData : Data;
            string strReturn = memoryTemplateEngine.Process(temData, TemplateName);
            return strReturn;
        }

        public void RenderString(string TemplateName) {
            HttpContext.Current.Response.Write(RenderStringProcess(null, TemplateName));
        }

        public void RenderString(IDictionary HasTableData, string TemplateName) {
            HttpContext.Current.Response.Write(RenderStringProcess(HasTableData, TemplateName));
        }

        public string RenderFileToString(string TemplateName) {
             return RenderFileProcess(null, TemplateName);
        }

        public string RenderFileToString(IDictionary HasTableData, string TemplateName) {
            return RenderFileProcess(HasTableData, TemplateName);
        }

        public string RenderStringToString(string TemplateName) {
            return RenderStringProcess(null, TemplateName);
        }

        public string RenderStringToString(IDictionary HasTableData, string TemplateName) {
            return RenderStringProcess(HasTableData, TemplateName);
        }

        
        #endregion

    }
}
